package com.stgy.lease.web.api.controller;

import com.stgy.lease.common.result.Result;
import com.stgy.lease.common.utils.JwtUtil;
import com.stgy.lease.model.domain.ViewAppointment;
import com.stgy.lease.web.api.service.ApartmentInfoService;
import com.stgy.lease.web.api.service.ViewAppointmentService;
import com.stgy.lease.web.api.vo.AppointmentDetailVo;
import com.stgy.lease.web.api.vo.AppointmentItemVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName:AppointmentController
 * Package:com.stgy.lease.web.api.controller
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/7 - 8:36
 */
@RestController
@RequestMapping("/app/appointment")
@Tag(name = "看房预约信息")
public class AppointmentController {

    @Autowired
    private ViewAppointmentService viewAppointmentService;

    ///app/appointment/saveOrUpdate POST
    @Operation(summary = "保存或更新看房预约")
    @PostMapping("saveOrUpdate")
    public Result<String> saveOrUpdate(@RequestBody ViewAppointment viewAppointment,
                                       @RequestHeader("access_token") String token,
                                       HttpServletRequest request) {
        Map<String, Object> map = JwtUtil.parseToken(token, request);
        Long userId = Long.parseLong(map.get("userId").toString());
        viewAppointment.setUserId(userId);
        return Result.build(viewAppointmentService.saveOrUpdate(viewAppointment));
    }

    ///app/appointment/listItem GET
    @Operation(summary = "查询个人预约看房列表")
    @GetMapping("/listItem")
    public Result<List<AppointmentItemVo>> listItem(@RequestHeader("access_token") String token,
                                                    HttpServletRequest request) {
        return Result.ok(viewAppointmentService.listItem(token, request));
    }

    //http://localhost:8888/app/appointment/getDetailById?id=1 GET
    @Operation(summary = "根据ID查询预约详情信息")
    @GetMapping("/getDetailById")
    public Result<AppointmentDetailVo> getDetailById(@RequestParam Long id) {
        return Result.ok(viewAppointmentService.getDetailById(id));
    }

}